package com.wooboo.server.ext.action.traffic;


import java.util.List;

import com.wooboo.server.ext.ActionBase;
import com.wooboo.server.ext.ApiError;
import com.wooboo.server.ext.Result;
import com.wooboo.server.ext.data.ChargeOrder;
import com.wooboo.server.ext.data.Traffic;
import com.wooboo.server.ext.data.TrafficGift;
import com.wooboo.server.ext.data.UserCompany;
import com.wooboo.server.ext.service.DbService;
import com.wooboo.server.ext.util.NumberUtil;
import com.wooboo.server.ext.util.Parameters;
import com.wooboo.server.ext.util.StringUtil;

import net.sf.json.JSONObject;

/**
 * @author www.wooboo.com.cn
 * 流量充值
 */
public class Charge extends ActionBase{

	public Integer id;
	public ChargeOrder order;
	DbService dbService = new DbService();
	public Charge(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		id = NumberUtil.getInteger(params.getFieldValue("id"));
		if(StringUtil.isEmpty(id)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	
	@Override
	public Result invokeService() throws ApiError {
		Result result = new Result();
		ChargeOrder order = dbService.findOrderById(id);
		if(null==order){
			throw ApiError.Type.INTERNAL_ERROR.toException("充值信息不存在！");
		}
		UserCompany com = dbService.findCompanyById(order.to_uid);
		if(null==com){
			throw ApiError.Type.INTERNAL_ERROR.toException("用户不存在！"+order.to_uid);
		}
		//System.out.println(order.api_status);
		if(Traffic.API_STATUS_APPLY!=order.api_status){//订单未充值流量
			if(Traffic.API_STATUS_NONE==order.api_status){//未处理
				boolean success = contractRegisterApply(com, order);
				if(success){
					companyFlowPkgRechargeApply(com, order);
				}
			}else if(Traffic.API_STATUS_REGISTER==order.api_status){
				companyFlowPkgRechargeApply(com, order);
			}
		}else{
			throw ApiError.Type.INTERNAL_ERROR.toException("流量汇接口不能重复充值。");
		}
		
	    return result;
	}
	
	
	public boolean contractRegisterApply(UserCompany com,ChargeOrder order) throws ApiError{
		DbService dbService = new DbService();
		List<TrafficGift> list = dbService.queryTrafficGiftByPlanId(order.getPlan_id());
		if(null==list||list.size()<=0){
			throw ApiError.Type.INVALID_PARAM.toException("充值合同未找到相应的流量包价格.");
		}
		JSONObject response = Traffic.companyContractRegisterApply(com,order,list);
		if(null==response){
			throw ApiError.Type.INVALID_PARAM.toException("调用流量汇接口【contractRegisterApply】失败！");
		}
		String status = response.optString("status");
		String code=response.optString("code");
		if("ok".equalsIgnoreCase(status) && "0".equals(code)){
			String contact_account_no = response.optJSONObject("result").getString("contact_account_no");
			 dbService.updateOrderOn(Traffic.API_STATUS_REGISTER, order.id,contact_account_no);
			 order.setContact_account_no(contact_account_no);
             return true;           
		}else{
			throw ApiError.Type.INVALID_PARAM.toException("调用流量汇接口【contractRegisterApply】失败！status="+status+",code="+code+",message="+response.optString("message")+"】");
		}
	}
	
	public void companyFlowPkgRechargeApply(UserCompany com,ChargeOrder order) throws ApiError{
		JSONObject response = Traffic.companyFlowPkgRechargeApply(com, order);
		if(null==response){
			throw ApiError.Type.INVALID_PARAM.toException("调用流量汇接口【companyFlowPkgRechargeApply】失败！");
		}
		String status = response.optString("status");
		String code=response.optString("code");
		if("ok".equalsIgnoreCase(status) && "0".equals(code)){
			//更新充值订单api状态 
			dbService.updateOrderOn(Traffic.API_STATUS_APPLY, order.id);

			//更新广告计划的合同编号
			dbService.updateAdPlanOnContact(order.contact_account_no, order.plan_id);
			 
		}else{
			throw ApiError.Type.INVALID_PARAM.toException("调用流量汇接口【companyFlowPkgRechargeApply】失败！status="+status+",code="+code+",message="+response.optString("message")+"】");
		}
	}
	
}
